package com.kai.xadmin.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kai.xadmin.entity.User;
import com.kai.xadmin.entity.dto.UserDto;
import com.kai.xadmin.service.UserService;
import com.kai.xadmin.utils.BeanCopyUtils;
import com.kai.xadmin.utils.ResponseResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author trouble
 * @since 2023-08-13
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@ApiOperation("处理用户登录")
	@PostMapping("/login")
	public ResponseResult login(UserDto userDto, @ApiIgnore HttpSession session) { // 加上 @ApiIgnore后，在测试接口时swagger会忽略该参数
		if (userDto != null) {
			User user = BeanCopyUtils.copyBean(userDto, User.class);
			User userInfo = userService.login(user);
			if (userInfo == null) {
				return ResponseResult.error("用户名或密码错误");
			}
			session.setAttribute("userInfo", userInfo);
			return ResponseResult.success(userInfo);
		}
		return ResponseResult.error("用户名或密码错误");
	}
}
